package com.skyworth.web.waiter;

import com.skyworth.utils.CookieUtil;
import com.skyworth.web.admin.user.domain.UserModel;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import static com.skyworth.web.common.Constants.*;

/**
 * Created by cody on 2016/9/23.
 */
public class WaiterCookie {

    /**
     * 写用户信息的cookie
     * @param loginkey
     * @param userModel
     * @param response
     */
    public static void writeSuccesCookie(String loginkey, UserModel userModel, HttpServletResponse response){
        try {
            CookieUtil.setCookie(COOKIE_USERNAME, URLEncoder.encode(userModel.getUsername(),"UTF-8"),response);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        CookieUtil.setCookie(COOKIE_USER_KEY,"" + loginkey,response);
        CookieUtil.setCookie(COOKIE_STORE_ID,"" + userModel.storeId,response);
        CookieUtil.setCookie(COOKIE_PASSWORD_KEY,userModel.password,response);
    }

    public static  void writeErrorCookie(HttpServletResponse response){
        CookieUtil.deleteCookie(COOKIE_USER_KEY,response);
        CookieUtil.deleteCookie(COOKIE_USERNAME,response);
        CookieUtil.deleteCookie(COOKIE_PASSWORD_KEY,response);
        CookieUtil.deleteCookie(COOKIE_STORE_ID,response);
    }


    public static void removeCookie(HttpServletResponse response) {
        writeErrorCookie(response);
    }
}
